package io.renren.modules.erp.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.erp.entity.BusinessBillEntity;
import io.renren.modules.erp.entity.BusinessBillInfoEntity;
import io.renren.modules.erp.entity.BusinessBillInfoVO;
import io.renren.modules.erp.service.BusinessBillInfoService;
import io.renren.modules.erp.service.BusinessBillService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.shiro.ShiroUtils;
import io.renren.common.utils.DateFormatUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 客户订单列表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-08-12 18:29:26
 */
@RestController
@RequestMapping("erp/businessbillinfo")
public class BusinessBillInfoController {
    @Autowired
    private BusinessBillInfoService businessBillInfoService;
    
    @Autowired
    private BusinessBillService businessBillService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("erp:businessbillinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = businessBillInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{businessBillInfoId}")
    @RequiresPermissions("erp:businessbillinfo:info")
    public R info(@PathVariable("businessBillInfoId") Long businessBillInfoId){
        BusinessBillInfoEntity businessBillInfo = businessBillInfoService.selectById(businessBillInfoId);

        return R.ok().put("businessBillInfo", businessBillInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("erp:businessbillinfo:save")
    public R save(@RequestBody BusinessBillInfoVO businessBillInfoVO){
    	String billDateStr=businessBillInfoVO.getBillDateStr();
    	Date billDate=DateFormatUtil.parseStrToDate(billDateStr,DateFormatUtil.DATE_FORMAT_YYYY_MM_DD);
    	
    	BusinessBillInfoEntity businessBill = new BusinessBillInfoEntity();
        BeanUtils.copyProperties(businessBillInfoVO,businessBill);
        businessBill.setBillDate(billDate);
        SysUserEntity userVO=ShiroUtils.getUserEntity();
		String userName=userVO.getUsername();
		businessBill.setCreateBy(userName);
        businessBill=businessBillInfoService.addBusinessBillInfo(businessBill);

        return R.ok().put("businessBillInfo", businessBill);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("erp:businessbillinfo:update")
    public R update(@RequestBody BusinessBillInfoVO businessBillInfoVO){
        String billDateStr=businessBillInfoVO.getBillDateStr();
    	Date billDate=DateFormatUtil.parseStrToDate(billDateStr,DateFormatUtil.DATE_FORMAT_YYYY_MM_DD);
    	BusinessBillInfoEntity businessBill = new BusinessBillInfoEntity();
        BeanUtils.copyProperties(businessBillInfoVO,businessBill);
        businessBill.setBillDate(billDate);
        ValidatorUtils.validateEntity(businessBill);
        businessBillInfoService.updateAllColumnById(businessBill);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("erp:businessbillinfo:delete")
    public R delete(@RequestBody Long[] businessBillInfoIds){
        businessBillInfoService.deleteBatchIds(Arrays.asList(businessBillInfoIds));
        businessBillService.deleteByInfoId(Arrays.asList(businessBillInfoIds));
        return R.ok();
    }

    
}
